package com.mrogrape.notepad.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EtchedBorder;

import com.mrogrape.io.DataUtils;
import com.mrogrape.notepad.common.ViewUtils;
import com.mrogrape.notepad.data.ViewContext;
import com.mrogrape.notepad.ui.editor.TextEditor;
import com.mrogrape.system.Environment;
import com.mrogrape.util.DateUtils;
import com.mrogrape.util.StringUtils;

public class FileProperyDialog extends JDialog{

	public FileProperyDialog(Frame frame){

		super(frame,true);

		this.initComponents();
	}

	public void showDialog(TextEditor editor){

		this.editor=editor;
		this.prepare();
		this.setVisible(true);
	}

	protected void okBtnActionPerformed(ActionEvent arg0){

		if(!this.encodingSelector.getSelectedItem().toString().equals(this.editor.getCharset())){

			int re=ViewUtils.showYesNoCancelDialog(this,"encoding changed","You have changed the file encoding, Apply change and reload it?");
			if(re == ViewUtils.YES_OPTION){

				this.editor.setCharset(this.encodingSelector.getSelectedItem().toString());
				if(this.editor.isFileExist() && !this.editor.isContentModified()){

					this.editor.loadFile();
				}else if(this.editor.isFileExist() && this.editor.isContentModified()){

					int isReload=ViewUtils.showOkCancelDialog(this,"Reload","You have changes not saved, Discard changes and reload it?");
					if(isReload == ViewUtils.OK_OPTION){
						this.editor.loadFile();
					}
				}
			}else if(re == ViewUtils.CANCEL_OPTION){

			return; }
		}

		this.setVisible(false);
	}

	protected void readOnlyCkActionPerformed(ActionEvent arg0){

		if(this.readOnlyCk.isSelected()){

			if(!this.editor.isContentModified() && this.editor.getFile() != null && this.editor.getFile().exists()){

				boolean result=this.editor.setReadOnly(true);
				if(!result){

					ViewUtils.showErrorMessageDialog(this,"无法设置文件为只读属性。");
					this.readOnlyCk.setText("Writable");
					this.readOnlyCk.setSelected(false);
				}else{
					this.readOnlyCk.setText("Read Only");
				}
			}
		}else{
			if(!this.editor.isContentModified() && this.editor.getFile() != null && this.editor.getFile().exists()){

				boolean result=this.editor.setReadOnly(false);
				if(!result){

					ViewUtils.showErrorMessageDialog(this,"无法设置文件为可写属性。");
					this.readOnlyCk.setText("Read Only");
					this.readOnlyCk.setSelected(true);
				}else{
					this.readOnlyCk.setText("Writable");
				}
			}
		}
	}

	private void initComponents(){

		JPanel panel=new JPanel();
		FlowLayout flowLayout=(FlowLayout) panel.getLayout();
		flowLayout.setHgap(10);
		flowLayout.setAlignment(FlowLayout.RIGHT);
		this.getContentPane().add(panel,BorderLayout.SOUTH);

		JButton okBtn=new JButton("OK");
		okBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0){
				FileProperyDialog.this.okBtnActionPerformed(arg0);
			}
		});
		panel.add(okBtn);

		JPanel contentPanel=new JPanel();
		contentPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED,null,null));
		this.getContentPane().add(contentPanel,BorderLayout.CENTER);

		JLabel locationLbl=new JLabel("Location");

		JLabel sizeLbl=new JLabel("Size");

		JLabel lastModifiedLbl=new JLabel("Last Modified");

		JLabel readOnlyLbl=new JLabel("ReadOnly");

		JLabel encodingLbl=new JLabel("Encoding");

		this.encodingSelector=new JComboBox(Environment.availableCharsets());

		this.locationView=new JTextField();
		this.locationView.setEditable(false);
		this.locationView.setColumns(10);

		this.sizeView=new JTextField();
		this.sizeView.setEditable(false);
		this.sizeView.setColumns(10);

		this.lastModifiedView=new JTextField();
		this.lastModifiedView.setEditable(false);
		this.lastModifiedView.setColumns(10);

		this.readOnlyCk=new JCheckBox("");
		this.readOnlyCk.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0){
				FileProperyDialog.this.readOnlyCkActionPerformed(arg0);
			}
		});

		JLabel changedLbl=new JLabel("Changed");

		this.changedCk=new JCheckBox("");
		this.changedCk.setEnabled(false);
		GroupLayout gl_contentPanel=new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addContainerGap().addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING).addComponent(encodingLbl).addComponent(lastModifiedLbl).addComponent(sizeLbl).addComponent(locationLbl).addComponent(readOnlyLbl).addComponent(changedLbl)).addGap(18).addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addComponent(this.sizeView,GroupLayout.DEFAULT_SIZE,264,Short.MAX_VALUE).addComponent(this.locationView,GroupLayout.DEFAULT_SIZE,264,Short.MAX_VALUE).addComponent(this.readOnlyCk).addComponent(this.lastModifiedView,GroupLayout.DEFAULT_SIZE,264,Short.MAX_VALUE).addComponent(this.changedCk).addComponent(this.encodingSelector,GroupLayout.PREFERRED_SIZE,172,GroupLayout.PREFERRED_SIZE)).addContainerGap()));
		gl_contentPanel.setVerticalGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addContainerGap().addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE).addComponent(locationLbl).addComponent(this.locationView,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE)).addPreferredGap(ComponentPlacement.RELATED).addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE).addComponent(sizeLbl).addComponent(this.sizeView,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE)).addPreferredGap(ComponentPlacement.UNRELATED).addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE).addComponent(lastModifiedLbl).addComponent(this.lastModifiedView,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE)).addPreferredGap(ComponentPlacement.UNRELATED).addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE).addComponent(readOnlyLbl).addComponent(this.readOnlyCk)).addPreferredGap(ComponentPlacement.UNRELATED).addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addComponent(changedLbl).addComponent(this.changedCk)).addGap(23).addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE).addComponent(encodingLbl).addComponent(this.encodingSelector,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE)).addContainerGap(40,Short.MAX_VALUE)));
		contentPanel.setLayout(gl_contentPanel);
		this.pack();
		ViewUtils.setCenterParent((Component) ViewContext.getMainFrame(),this);
	}

	private void prepare(){

		this.setTitle("View Properties: " + this.editor.getPlainTitle());
		File file=this.editor.getFile();

		if(file != null){

			this.locationView.setText(file.getAbsolutePath());
			this.sizeView.setText(DataUtils.descBytesLength(file.length()));
			this.lastModifiedView.setText(DateUtils.format(file.lastModified(),"yyyy-MM-dd HH:mm:ss"));

			if(this.editor.isContentModified() && !file.exists()){

				this.readOnlyCk.setEnabled(false);
				this.readOnlyCk.setText("Unsupported");
			}else{

				this.readOnlyCk.setEnabled(true);
			}

			if(file.canWrite()){
				this.readOnlyCk.setText("Writable");
				this.readOnlyCk.setSelected(false);
			}else{
				this.readOnlyCk.setText("Read Only");
				this.readOnlyCk.setSelected(true);
			}

			if(this.editor.isFileChanged()){

				this.changedCk.setSelected(true);
				this.changedCk.setText("file was changed after open it");
			}else{

				this.changedCk.setSelected(false);
				this.changedCk.setText("file not changed after open it");
			}

		}else{

			this.locationView.setText("Unsupported");
			this.sizeView.setText("Unsupported");
			this.lastModifiedView.setText("Unsupported");

			this.readOnlyCk.setEnabled(false);
			this.readOnlyCk.setText("Unsupported");

			this.changedCk.setText("Unsupported");
		}

		String selectedCharset = StringUtils.isEmpty(this.editor.getCharset())? System.getProperty("file.encoding"): this.editor.getCharset();
		this.encodingSelector.setSelectedItem(selectedCharset);
		if(!selectedCharset.equals(this.encodingSelector.getSelectedItem().toString())){
			this.encodingSelector.addItem(selectedCharset);
			this.encodingSelector.setSelectedItem(selectedCharset);
		}

	}

	/** serialVersionUID */
	private static final long serialVersionUID=1L;
	private JTextField locationView;
	private JTextField sizeView;
	private JTextField lastModifiedView;

	private TextEditor editor;
	private JCheckBox readOnlyCk;
	private JCheckBox changedCk;
	private JComboBox encodingSelector;
}
